import { reactive } from 'vue'
interface ZhuceUser {
  name: string;
  email: string;
  password: string;
  password2: string;
  role: string;
}
interface ZhuceRules {
  name: ({
      message: string;
      trigger: string;
      required: boolean;
  } | {
      min: number;
      max: number;
      message: string;
      trigger: string;
  })[];
  email: {
      type: string;
      message: string;
      trigger: string;
      required: boolean;
  }[];
  password: ({
    message: string;
    trigger: string;
    required: boolean;
  } | {
    min: number;
    max: number;
    message: string;
    trigger: string;
  })[];
  password2: ({
    message: string;
    trigger: string;
    required: boolean;
} | {
    min: number;
    max: number;
    message: string;
    trigger: string;
} | {
  validator: (rule:ZhuceRules,value:string,callback: any)=>void;
  trigger: string;
})[];
}
export const zhuceUser = reactive<ZhuceUser>({
  name:"",
  email:"",
  password:"",
  password2:"",
  role: "",
})
const validatePass2 = (rule: ZhuceRules, value: string, callback: any) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== zhuceUser.password) {
    callback(new Error("两次输入密码不一致！"))
  } else {
    callback()
  }
}
export const zhuceRules = reactive<ZhuceRules>({
  name: [
    { message:"请填写用户名", trigger: 'blur',required: true },
    { min:2,max:30, message:"用户名长度应在2-30字符之间",trigger: 'blur' }
  ],
  email: [
    { type:"email",message:"邮箱格式不正确", trigger: 'blur',required: true }
  ],
  password: [
    { message:"请填写密码", trigger: 'blur',required: true },
    { min:4,max:30,message:"密码长度应为4-30字符之间",trigger:'blur'}
  ],
  password2: [
    { message:"请确认密码", trigger: 'blur',required: true },
    { min:4,max:30,message:"密码长度应为4-30字符之间",trigger:'blur'},
    { validator: validatePass2, trigger: 'blur'},
  ],
})